import tkinter as tk#导入
from tkinter import *
import re
from tkinter import ttk


from LengthTransition import LengthTransition
from calculate import Calculator

tr=LengthTransition()
def get_opt(val):
    if tr.unit_1=='':
        tr.setUnit_1(val)
    elif tr.unit_2=='':
        tr.setUnit_2(val)

def convert(envet):
    global data_out
    tr.setUnit_1(box2.get())
    tr.setUnit_2(box3.get())
    data_out=tr.transition(num.get())
    print(data_out)
    ans.delete(0,len(ans.get()))
    ans.insert('insert',data_out)
# 实例化object,建立窗口window
window = tk.Tk()
# 给窗口的可视化取名
window.title('长度单位转换')
# 设定窗口大小(长*宽)
window.geometry('360x699')


#Entry为文本输入域
num = tk.Entry(window, width=20, show=None, font=('Arial', 14),justify='right')  # 显示成明文形式
num.grid(row=0, column=0,  sticky='nw',ipadx=5, ipady=5)
box2 = ttk.Combobox(window)
box2['values']=['米m','厘米cm','千米km','分米dm','毫米mm','英尺ft','英寸in']
box2.bind("<<ComboboxSelected>>", convert) #或者选择下拉框内容后，生成对应结果
box2.grid(row=0,column=1, sticky='nw',ipadx=5,ipady=5)

label1=tk.Label(window,text="等于")
label1.grid(row=2,column=0,sticky='nw',ipadx=5)

ans=tk.Entry(window,width=20, show=None, font=('Arial', 14),justify='right')
ans.grid(row=3,  column=0,sticky='nw',ipadx=5)
box3 = ttk.Combobox(window)
box3['values']=['米m','厘米cm','千米km','分米dm','毫米mm','英尺ft','英寸in']
box3.bind("<<ComboboxSelected>>", convert) #或者选择下拉框内容后，生成对应结果
box3.grid(row=3,column=1, sticky='nw',ipadx=5)

scientific_computing_buttons=[
    '7','8','9','x',
    '4','5','6','-',
    '1','2','3','+',
    '0','.','=','\u232B'
]
#2024/11/4 李坤修改下列代码,将按钮和函数绑定,新增函数列表
calculator=Calculator()
scientific_computing_methods=[
    lambda: button_on_click('7'),lambda: button_on_click('8'),lambda: button_on_click('9'),lambda: button_on_click('*'),
    lambda: button_on_click('4'),lambda: button_on_click('5'),lambda: button_on_click('6'),lambda: button_on_click('-'),
    lambda: button_on_click('1'),lambda: button_on_click('2'),lambda: button_on_click('3'),lambda: button_on_click('+'),
    lambda: button_on_click('0'),lambda: button_on_click('.'),lambda: button_on_click('='),lambda: button_on_click('d')
]

s_row_value=4
s_col_value=0

def show():
    ans.config(text=calculator.showString())
def button_on_click(str):
    #正则表达式匹配
    if re.match(r'^[0-9]$', str):
        calculator.addNumber(str)
        num.insert('insert',str)
    elif str == ".":
        calculator.addNumber(str)
        num.insert('insert',str)
    elif str=="=":
        calculator.eqv()
        num.delete(0,len(num.get()))
        num.insert('insert',calculator.showString())
    elif str == "d":
        calculator.deleteone()
        num.delete(len(num.get())-1,len(num.get()))
    else:
        calculator.addMethod(str)
        num.insert('insert',str)

    show()


for button, method in zip(scientific_computing_buttons, scientific_computing_methods):
    #callable内置函数判断是否为方法
    if callable(method):
        button=Button(window,text=button,width=10,command=method)
    else:
        button = Button(window, text=button, width=10)
    button.grid(row=s_row_value, column=s_col_value, padx=3, pady=3, ipadx=3, ipady=3)
    s_col_value+=1
    if s_col_value>3:
        s_row_value+=1
        s_col_value=0
for i in range(5):
    window.columnconfigure(i, weight=1)  # 每列权重都是1，因此它们会均匀伸展

for i in range(10):
    window.rowconfigure(i, weight=1)  # 每行权重都是1，因此它们也会均匀伸展

window.mainloop()#循环,使得每次点击都有反馈